﻿using PTTPOS_Solution.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace PTTPOS_Solution
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnlogin_MouseDown(object sender, MouseEventArgs e)
        {
            btnlogin.BackColor = Color.Gray;
        }

        private void btnlogin_MouseUp(object sender, MouseEventArgs e)
        {
            btnlogin.BackColor = Color.FromArgb(224, 224, 224);
            login();
        }

        private void login()
        {
            if (checkValidate())
            {
                if (Account_DA.login(txtUsername.Text, GetMd5Hash(txtPassword.Text)))
                {
                    DialogResult = DialogResult.Yes;
                }
                else
                    lblErr.Text = "Thông tin tài khoản hoặc quyền hạn chưa đúng";
            }
        }
        static string GetMd5Hash(string input)
        {
            MD5 md5Hash = MD5.Create();
            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }
        private bool checkValidate()
        {
            bool chk = false;
            if (string.IsNullOrEmpty(txtUsername.Text) | string.IsNullOrEmpty(txtPassword.Text))
            {
                lblErr.Text = "Tài khoản và mật khẩu không được trống";
            }
            else
                chk = true;
            return chk;
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            pnContent.Location = new Point(this.Size.Width / 2 - pnContent.Size.Width / 2, this.Size.Height / 2 - pnContent.Height);
            keyboard.Visible = true;
            keyboard.Location = new Point(pnContent.Location.X + (pnContent.Width / 2) - (keyboard.Width / 2), pnContent.Location.Y + pnContent.Height + 5);
        }

        private void txtUsername_Click(object sender, EventArgs e)
        {
            keyboard.Visible = true;
            keyboard.Location = new Point(pnContent.Location.X + (pnContent.Width / 2) - (keyboard.Width / 2), pnContent.Location.Y + pnContent.Height + 5);
        }

        private void txtPassword_Click(object sender, EventArgs e)
        {
            keyboard.Visible = true;
            keyboard.Location = new Point(pnContent.Location.X + (pnContent.Width / 2) - (keyboard.Width / 2), pnContent.Location.Y + pnContent.Height + 5);
        }
        private bool dragging = false;
        private Point dragCursorPoint, dragFormPoint;
        private void keyboard_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            dragCursorPoint = Cursor.Position;
            dragFormPoint = this.Location;
        }

        private void keyboard_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;
        }

        private void keyboard_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
                this.Location = Point.Add(dragFormPoint, new Size(dif));
            }
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                login();
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                login();
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {

        }
    }
}
